﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.FileManipulation.Properties;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FilePathMappingComponent:WorkflowComponentBase
    {
        #region props

        private FilePathMappingInstruction _Instruction;
        /// <summary>an object that encapsulate instruction settings.</summary>
        public override InstructionBase Instruction
        {
            get { return this._Instruction; }
            set { this._Instruction = (FilePathMappingInstruction) value; }
        }

        private FilePathMappingOutput _Output;
        /// <summary>an object to encapsulate output</summary>
        public override ResultBase Output
        {
            get { return this._Output; }
            set { this._Output = (FilePathMappingOutput) value;}
        }

        /// <summary></summary>
        public string FolderPath
        {
            get
            {
                return this._Instruction.FolderPath;
            }
            set
            {
                this._Instruction.FolderPath = value;
            }
        }

        /// <summary></summary>
        public string FileNameSearchPattern
        {
            get
            {
                return this._Instruction.FileNameSearchPattern;
            }
            set
            {
                this._Instruction.FileNameSearchPattern = value;
            }
        }

        /// <summary></summary>
        public bool IncludeSubFolders
        {
            get
            {
                return this._Instruction.IncludeSubFolders;
            }
            set
            {
                this._Instruction.IncludeSubFolders = value;
            }
        }

        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._Instruction.ConnStr;
            }
            set
            {
                this._Instruction.ConnStr = value;
            }
        }

        /// <summary></summary>
        public string TblName
        {
            get
            {
                return this._Instruction.TblName;
            }
            set
            {
                this._Instruction.TblName = value;
            }
        }

        /// <summary></summary>
        public string IDField
        {
            get
            {
                return this._Instruction.IDField;
            }
            set
            {
                this._Instruction.IDField = value;
            }
        }

        /// <summary></summary>
        public string FilePathField
        {
            get
            {
                return this._Instruction.FilePathField;
            }
            set
            {
                this._Instruction.FilePathField = value;
            }
        }

        /// <summary>regex pattern to grab id value from file name</summary>
        public string FileNameMatchPattern
        {
            get
            {
                return this._Instruction.FileNameMatchPattern;
            }
            set
            {
                this._Instruction.FileNameMatchPattern = value;
            }
        }

        /// <summary>default to 1</summary>
        public int CatchIndex
        {
            get
            {
                return this._Instruction.CatchIndex;
            }
            set
            {
                this._Instruction.CatchIndex = value;
            }
        }

        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public FilePathMappingComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.File_Tasks.ToString();
            this.ComponentName = "File path mapping";
            this.Description = "Map file path based on file name pattern matches";
            this.ComponentIcon = Resources.openedFolder;
            this.ComponentImage = Resources.Folder;
            

            this._Instruction = new FilePathMappingInstruction(
                this.ComponentID, Guid.Empty, string.Empty, "*.*", true,
                string.Empty, string.Empty, string.Empty, string.Empty,
                string.Empty, 1);
            this._Output = new FilePathMappingOutput(
                this.ComponentID, Guid.Empty, 0);
        }
        #endregion

        #region Overrides of WorkflowComponentBase


        /// <summary><para>must implementable method to serialize activity settings to xml</para></summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FolderPath", this.FolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "NameSearchPattern", this.FileNameSearchPattern);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IncludeSubFolders", this.IncludeSubFolders.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TblName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IDField", this.IDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilePathField", this.FilePathField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "NameMatchPattern", this.FileNameMatchPattern);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CatchIndex", this.CatchIndex.ToString());
            
            return xNode;
        }

        /// <summary><para>subclass must implement this method to instantiate activity from xml</para></summary>
        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FilePathMappingComponent component = base.Instantiate(xNode) as FilePathMappingComponent;

                component.FolderPath = XmlDataUtil.GetAttributeValue(xNode, "FolderPath", "");
                component.FileNameSearchPattern = XmlDataUtil.GetAttributeValue(xNode, "NameSearchPattern", "*.*");
                component.IncludeSubFolders =
                    bool.Parse(XmlDataUtil.GetAttributeValue(xNode, "IncludeSubFolders",
                                                             true.ToString()));
                component.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                component.TblName = XmlDataUtil.GetAttributeValue(xNode, "TableName", "");
                component.IDField = XmlDataUtil.GetAttributeValue(xNode, "IDField", "");
                component.FilePathField = XmlDataUtil.GetAttributeValue(xNode, "FilePathField", "");
                component.FileNameMatchPattern = XmlDataUtil.GetAttributeValue(xNode, "NameMatchPattern", "");
                component.CatchIndex = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "CatchIndex", "1"));

                return component;
            }
            else
            {
                throw new Exception("Invalid type to instantiate");
            }
        }

        #endregion
    }
}
